<?php
	/*
	 * 引用一个类的属性和方法就使用 -> 符号	
	 * -> 这个箭头也可以是调用类中的函数
	 */
	header("Content-Type: text/html;charset=utf-8");

	$pageSize = $_POST['pageSize']?:10;   //预定每页显示的信息条数
	$pageNum = $_POST['pageNum']?:1;     //当前页数

	$servername = "localhost";
	$username = "root";
	$password = "456789";
	$dbname = "mysqlstudy";

	$conn = new mysqli($servername,$username,$password,$dbname);
	if($conn -> connect_error){
		die("连接失败:" . $conn -> connect_error);
	}
	mysqli_query($conn, "set names utf8");

	//显示总行数的函数
	function allLines($conn){
		$rs = "SELECT COUNT(*) num from tp_order";
		$r = $conn -> query($rs);
		$obj = $r -> fetch_object();
		return $obj -> num;
	}

	$allLines = allLines($conn);  //总行数
	$endPage = ceil($allLines/$pageSize);   //总页数

	//分页的函数
	function pages($pageNum=1,$pageSize=10,$conn){
		$array = array();
		$rs = "SELECT * from tp_order limit " . (($pageNum-1)*$pageSize) . "," . $pageSize;
		$r = $conn -> query($rs);
		while($obj = $r -> fetch_object()){
			$array[] = $obj;
		}
		return $array;
	}

	$pageData = pages($pageNum,$pageSize,$conn);  //查询当页的数据
	
	echo json_encode(array('pageData' => $pageData, 'endPage' => $endPage, 'pageNum' => $pageNum));

?>

